import { userlist, usercount } from "../../request/api.js"

const state = {
  list: [],
  total: 0,
  //没页展示的条数
  size:2,
  //当前页码数
  page:1
}

const mutations = {
  changeList(state, arr) {
    state.list = arr
  },
  changeTotal(state, num) {
    state.total = num
  },
  changePage(state, num) {
    state.page = num
  }
}
const actions = {
  //请求列表数据
  asyncChangeList(context) {
    console.log(context.state.page);
    userlist({ page:context.state.page , size:context.state.size}).then(res => {
let arr =res.data.list?res.data.list:[]
if(arr.length<=0 &&context.state.page>1){
  context.commit("changePage",context.state.page-1)
  context.dispatch("asyncChangeList")
}
      context.commit("changeList", arr)
    })
  },
  asyncChangeTotal(context){
    usercount().then(res=>{
      context.commit("changeTotal",res.data.list[0].total)
    })
  }, 
  asyncChangePage(context,num){
   
      context.commit("changePage",num)
      context.dispatch("asyncChangeList")
  }
}

const getters = {
  list(state) {
    return state.list
  },
  total(state) {
    return state.total
  },
  size(state){
    return state.size
  }
}

export default {
  state, mutations, actions, getters, namespaced: true
}